package cn.signalpha.modules.social.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.social.service.CommentService;
import cn.signalpha.modules.social.service.LikesService;
import cn.signalpha.modules.social.service.TopicService;
import cn.signalpha.modules.social.vo.CommentVO;
import cn.signalpha.modules.social.vo.TopicUserVO;
import cn.signalpha.modules.social.vo.TopicVO;
import cn.signalpha.modules.social.wrapper.TopicWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SOCIAL_NAME + "/wap/user")
@Api(value = "用户", tags = "用户")
public class ApiUserController extends BaseController {

	private final UserService userService;
	private final LikesService likesService;
	private final TopicService topicService;
	private final CommentService commentService;

	@GetMapping("/info")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "用户信息", notes = "")
	public R<TopicUserVO> info() {
		User user = userService.getById(getUser().getUserId());
		TopicUserVO topicUserVO = new TopicUserVO();
		topicUserVO.setUserId(user.getId());
		topicUserVO.setUserAvatar(user.getAvatar());
		topicUserVO.setUserNickName(user.getNickName());
		topicUserVO.setTopicCount(topicService.countByParams(Kv.create().set("userId", user.getId())));
		topicUserVO.setLikeCount(likesService.countByParams(Kv.create().set("userId", user.getId())));
		topicUserVO.setLikeMyCount(likesService.countByParams(Kv.create().set("toUserId", user.getId())));
		topicUserVO.setCommentCount(commentService.countByParams(Kv.create().set("fromUserId", user.getId())));
		return R.data(topicUserVO);
	}

	@GetMapping("/topic/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "我的作品", notes = "")
	public R<IPage<TopicVO>> targetPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("userId", getUser().getUserId().toString());
		IPage<TopicVO> page = topicService.selectTopicPage(Condition.getPage(query), params);
		return R.data(TopicWrapper.build().page(page, getUser().getUserId()));
	}

	@GetMapping("/likes/page")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "点赞作品", notes = "")
	public R<IPage<TopicVO>> likesPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("userId", getUser().getUserId().toString());
		IPage<TopicVO> page = likesService.selectLikesOnTopicPage(Condition.getPage(query), params);
		return R.data(TopicWrapper.build().page(page, getUser().getUserId()));
	}

	@GetMapping("/comment/page")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "评论记录", notes = "")
	public R<IPage<CommentVO>> commentPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("fromUserId", getUser().getUserId().toString());
		return R.data(commentService.selectCommentPage(Condition.getPage(query), params));
	}

}

